package xiegd.study.localTime;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.junit.platform.commons.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;

/**
 * @Author: xieguangda
 * @Description:
 * @Date: 2023/3/21 16:35
 */
public class DateUtils {

    public static SimpleDateFormat getSimpleDateFormat(String pattern) {
        return new SimpleDateFormat(pattern);
    }


    public static void main(String[] args) {
        LocalDateTime localDateTime = maxLocalDateTime("2022-03-03");

        LocalDateTime localDateTime1 = minLocalDateTime("2022-03-04");
        System.out.println("localDateTime:" + localDateTime);

        System.out.println("最小：" + localDateTime1);

    }

    public static LocalDateTime parseLocalDateTime(String dateStr) {
        DateTime parse = DateUtil.parse(dateStr);
        Instant instant = parse.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
        return localDateTime;
    }

    public static LocalDateTime maxLocalDateTime(String dateStr){
        if(StringUtils.isBlank(dateStr)){
            return null;
        }
        LocalDateTime localDateTime = parseLocalDateTime(dateStr);
        LocalDateTime with = localDateTime.with(LocalTime.MAX);

        return with;
    }

    /**
     * @description: 获取当日最小时间
     * @param: dateStr
     * @return: LocalDateTime
     * @author xieguangda
     * @date: 2023/11/1 15:42
     */
    public static LocalDateTime minLocalDateTime(String dateStr){
        if(StringUtils.isBlank(dateStr)){
            return null;
        }
        LocalDateTime localDateTime = parseLocalDateTime(dateStr);
        LocalDateTime with = localDateTime.with(LocalTime.MIN);
        return with;
    }
}
